const url_all = {
	'DEV': '/nongmao', // 开发http://10.140.0.52:8080https://1yj-rt.gfox.ltd http://10.141.0.117:8080
	'ENV': 'http://localhost:8080'
}

export const BASEURL = url_all['ENV'];

// 发起请求
function handleRequest(options, resolve, reject) {
	let cookie = uni.getStorageSync('X-Token')
	//console.log('cookie',cookie)
	let baseParams = {
		// 请求接口地址
		url: BASEURL + options.url,
		// 请求类型
		method: options.method || 'GET',
		// 请求参数
		data: options.data,
		// 请求头
		header: {
			// 'X-MINESPAN-SESSION': token,
			//'Content-Type': 'application/json',
			// 'Cookie': `X-Token=${cookie}`
		}
	}
	uni.request({
		...baseParams,
		success: (response) => {
			
			const res = response.data;
			if (!res.success) {
				uni.showToast({
					title: res.msgContent,
					icon: 'none'
				})
				if(res.msgCode == 'common.starter.eduservice.needLogin'){
					uni.navigateTo({
						url: '/pages/login/login'
					})
				}
				return;
			}
			return resolve(res)
		},
		fail: (fail) => {
			console.log('fail', fail)
			return reject(fail);
		},
		complete() {
			
		}
		
	})
}

//公共上传单张图片
function handleUpload(options, resolve, reject) {
	let cookie = uni.getStorageSync('X-Token')
	let baseParams = {
		// 请求接口地址
		url: BASEURL + options.url,
		// 请求类型
		filePath: options.filePath, //图片路径
		// 请求参数
		name: 'file',
		// 请求头
		header: {
			'Cookie': `X-Token=${cookie}`,
			//'Content-Type': 'multipart/form-data'
		},
		/* formData:{
			"formData":{
				"sourceType":0,
				"pageSize":"10",
				"pageNum":"1"
			}
		}, */
	}
	uni.uploadFile({
		...baseParams,
		success: (res) => {
			if (JSON.parse(res.data).success) {
				return resolve(JSON.parse(res.data))
			} else {
				uni.showToast({
					title: JSON.parse(res.msgContent),
					icon: 'none'
				})
				return
			}
		},
		fail: (fail) => {
			console.log('fail', fail)
			return reject(fail);
		}
	})
}
//公共上传多张图片
/* function handleBatchUpload(options, resolve, reject) {
	let baseParams = {
		// 请求接口地址
		url: BASEURL + options.url,
		// 请求类型
		files: options.files,
		filePath: options.filePath,
		// 请求参数
		name: 'file',
		// 请求头
		header: {
			// 'X-MINESPAN-SESSION': token,
			'Content-Type': 'multipart/form-data'
		}
	}
	console.log('baseParams',baseParams)
	uni.uploadFile({
		...baseParams,
		success: (res) => {
			if (JSON.parse(res.data).success) {
				return resolve(JSON.parse(res.data))
			} else {
				uni.showToast({
					title: JSON.parse(res.msgContent),
					icon: 'none'
				})
				return
			}
		},
		fail: (fail) => {
			console.log('fail', fail)
			return reject(fail);
		}
	})
} */

function handleDownload(options, resolve, reject) {
	let baseParams = {
		// 请求接口地址
		url: BASEURL + options.url,
		// 请求类型
		filePath: options.filePath, //保存路径
	}
	uni.downloadFile({
		...baseParams,
		success: (res) => {
			if (res.statusCode === 200) {
				return resolve(res)
			}else{
				uni.showToast({
					title: res.errMsg,
					icon: 'none'
				})
				return
			}
		},
		fail: (fail) => {
			console.log('fail', fail)
			return reject(fail);
		}
	})
}

//公共文件下载

export const request = (options) => {
	return new Promise((resolve, reject) => {
		handleRequest(options, resolve, reject)
	})
}

export const UploadFile = (options) => {
	return new Promise((resolve, reject) => {
		handleUpload(options, resolve, reject)
	})
}

/* export const BatchUpload = (options) => {
	return new Promise((resolve, reject) => {
		handleBatchUpload(options, resolve, reject)
	})
} */

export const DownloadFile = (options) =>{
	return new Promise((resolve, reject) => {
		handleDownload(options, resolve, reject)
	})
}
